package cn.tqfeiyang.springplus.warehouse.service.impl;

import cn.tqfeiyang.springplus.warehouse.entity.CompanyEntity;
import cn.tqfeiyang.springplus.warehouse.mapper.CompanyMapper;
import cn.tqfeiyang.springplus.warehouse.service.CompanyService;
import cn.tqfeiyang.springplus.warehouse.request.CompanyDTO;
import cn.tqfeiyang.springplus.warehouse.response.CompanyVO;
import cn.tqfeiyang.springplus.warehouse.request.CompanySTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.tqfeiyang.springplus.framework.conversion.ConversionService;
import cn.tqfeiyang.springplus.framework.exception.BusinessException;
import cn.tqfeiyang.springplus.framework.response.ResultCode;
import cn.tqfeiyang.springplus.framework.pagination.PageHelper;
import cn.tqfeiyang.springplus.framework.pagination.PageData;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;

/**
 * 公司服务实现类
 *
 * @author tqfeiyang
 * @since 2024-08-01
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, CompanyEntity> implements CompanyService {

    /**
     * 对象转换服务
     */
    @Resource
    private ConversionService conversionService;

    /**
     * 公司映射接口
     */
    @Resource
    private CompanyMapper companyMapper;

    /**
     * 分页查询公司
     *
     * @param companySTO 查询数据
     * @return 公司列表
     */
    @Override
    public PageData<CompanyVO> selectCompanyPage(CompanySTO companySTO) {
        Assert.notNull(companySTO, "companySTO不能为空");
        PageHelper<CompanyEntity> pageHelper = new PageHelper<>(companySTO);
        return pageHelper.selectPage(companyMapper, CompanyVO.class);
    }

    /**
     * 查询公司
     *
     * @param companyId 公司id
     * @return 公司信息
     */
    @Override
    public CompanyVO selectCompanyInfo(Long companyId) {
        Assert.notNull(companyId, "companyId不能为空");
        CompanyEntity companyEntity = checkCompanyExists(companyId);
        return conversionService.map(companyEntity, CompanyVO.class);
    }

    /**
     * 新建公司
     *
     * @param companyDTO 请求数据
     * @return 公司id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createCompany(CompanyDTO companyDTO) {
        Assert.notNull(companyDTO, "companyDTO不能为空");
        CompanyEntity companyEntity = conversionService.map(companyDTO, CompanyEntity.class);
        if (companyMapper.insert(companyEntity) == 0) {
            throw new BusinessException(ResultCode.ENTITY_INSERT_ERROR);
        }
        return companyEntity.getCompanyId();
    }

    /**
     * 修改公司
     *
     * @param companyId 公司id
     * @param companyDTO 请求数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCompany(Long companyId, CompanyDTO companyDTO) {
        Assert.notNull(companyId, "companyId不能为空");
        Assert.notNull(companyDTO, "companyDTO不能为空");
        CompanyEntity originalEntity = checkCompanyExists(companyId);
        CompanyEntity companyEntity = conversionService.map(companyDTO, CompanyEntity.class);
        companyEntity.setCompanyId(companyId);
        companyEntity.setVersion(originalEntity.getVersion());
        if (companyMapper.updateById(companyEntity) == 0) {
            throw new BusinessException(ResultCode.ENTITY_UPDATE_ERROR);
        }
    }

    /**
     * 删除公司
     *
     * @param companyId 公司id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCompany(Long companyId) {
        Assert.notNull(companyId, "companyId不能为空");
        checkCompanyExists(companyId);
        if (companyMapper.deleteById(companyId) == 0) {
            throw new BusinessException(ResultCode.ENTITY_DELETE_ERROR);
        }
    }

    /**
     * 检查公司实体是否存在
     *
     * @param companyId 公司id
     * @return 公司实体
     */
    protected CompanyEntity checkCompanyExists(Long companyId) {
        CompanyEntity companyEntity = companyMapper.selectById(companyId);
        if (companyEntity == null) {
            throw new BusinessException(ResultCode.ENTITY_NOT_EXISTS);
        }
        return companyEntity;
    }

}
